﻿using System;
using System.Windows.Forms;
using Notepad_X;
using Notepad_X.Plugin;

namespace GoogleTranslateSidebarPlugin
{
    public class plugin : IPlugin
    {
        Translate PluginContainer;

        SideBarPlugin.plugin Sidebar = null;

        public string Name
        { get { return "Google Translate Sidebar Plugin"; } }
        public string Author
        { get { return "NohponeX"; } }
        public string Description
        { get { return "Notepad X Sidebar Plugin\n\r Google Translate Sidebar Plugin\nRequires version 2.2 or newer"; } }
        public string Version
        { get { return "1.1"; } }
        public bool HasSettings { get { return false; } }


        public void Initialize()
        {
            //try
            //{
                Sidebar = SideBarPlugin.Functions.GetSidebarPlugin();
                if (Sidebar != null)
                {
                    if (!Sidebar.Controls.ContainsKey("Google Translate"))
                    { 
                        PluginContainer = new Translate();
                        PluginContainer.Text = "Google Translate";
                        PluginContainer.Name = "Google Translate";
                        PluginContainer.Dock = DockStyle.Fill;
                        Sidebar.AddSidebarPlugin(PluginContainer);
                    }
                }
            //} catch (Exception e) { MessageBox.Show(e.Message); }
        }
        public static void SaveSettings(Translate Cntainer) {
            //if (Sidebar != null ||  PluginContainer != null)  return ;
             
          //  if (!Sidebar.Controls.ContainsKey(PluginContainer.Name)) return;
                  
            bool found = false;
            for (int i = 0; i < Notepad_X.Main.app_settings.pluginsSettings.Count; i++) {
                if (Notepad_X.Main.app_settings.pluginsSettings[i].name.ToLower() == "googletranslatesidebarplugin") {
                    bool valueFromFound = false;
                    bool valueToFound = false;
                    for (int j = 0; j < Notepad_X.Main.app_settings.pluginsSettings[i].values.Count; j++) {
                        if (Notepad_X.Main.app_settings.pluginsSettings[i].values[j].Key == "from") {
                            Notepad_X.Main.app_settings.pluginsSettings[i].values[j] = new System.Collections.Generic.KeyValuePair<string, string>("from", Cntainer.from_Combo.SelectedItem.ToString());
                            valueFromFound = true;
                        }
                        if (Notepad_X.Main.app_settings.pluginsSettings[i].values[j].Key == "to") {
                            Notepad_X.Main.app_settings.pluginsSettings[i].values[j] = new System.Collections.Generic.KeyValuePair<string, string>("to", Cntainer.to_Combo.SelectedItem.ToString());
                            valueToFound = true;
                        }
                    }
                    if (!valueFromFound) Notepad_X.Main.app_settings.pluginsSettings[i].values.Add(new System.Collections.Generic.KeyValuePair<string,string>("from", Cntainer.from_Combo.SelectedItem.ToString()));
                    if (!valueToFound) Notepad_X.Main.app_settings.pluginsSettings[i].values.Add(new System.Collections.Generic.KeyValuePair<string,string>("to", Cntainer.to_Combo.SelectedItem.ToString()));
                    found = true;
                    break;
                }
            }
                if(!found){
                    Notepad_X.Classes.pluginSetting s;
                    s.name ="GoogleTranslateSidebarPlugin";
                    s.values = new System.Collections.Generic.List<System.Collections.Generic.KeyValuePair<string,string>>(2);
                    s.values.Add(new System.Collections.Generic.KeyValuePair<string,string>("from", Cntainer.from_Combo.SelectedItem.ToString()));
                    s.values.Add(new System.Collections.Generic.KeyValuePair<string,string>("to", Cntainer.to_Combo.SelectedItem.ToString()));
                    Notepad_X.Main.app_settings.pluginsSettings.Add(s);
                }
        }
        public void Dispose()
        {
            try
            {
                Sidebar = SideBarPlugin.Functions.GetSidebarPlugin();
                if (Sidebar != null && PluginContainer != null)
                {
                    if (Sidebar.Controls.ContainsKey(PluginContainer.Name))
                    {
                        Sidebar.RemoveSidebarPlugin(PluginContainer);
                    }
                }
            }
            catch { }
        }
        public void Settings() { }
    }
}
